import {
  Component,
  Injector,
  ViewEncapsulation,
  ViewChild
} from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
  TechFileInfoServiceProxy, AlterationInvoiceServiceProxy,
} from '@shared/service-proxies/service-proxies';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import * as moment from 'moment';
import { FileDownloadService } from '@shared/utils/file-download.service';

@Component({
  templateUrl: './alterationinvoice.component.html',
  encapsulation: ViewEncapsulation.None,
  styleUrls: ['./alterationinvoice.component.less'],
  animations: [appModuleAnimation()]
})
export class AlterationInvoicesComponent extends AppComponentBase {
  @ViewChild('dataTable') dataTable: Table;
  @ViewChild('paginator') paginator: Paginator;

  advancedFiltersAreShown = false;
  anyncing = false;
  filterText = '';

  startFilterDate: Date;
  endFilterDate: Date;

  constructor(
    injector: Injector,
    private _alterServiceProxy: AlterationInvoiceServiceProxy,
    private _fileDownloadService: FileDownloadService,
  ) {
    super(injector);
  }
  exportToExcel() {
    this._alterServiceProxy.getAlterationInvoiceInfosToExcel(
      this.filterText,
      this.startFilterDate ? moment(this.startFilterDate) : null,
      this.endFilterDate ? moment(this.endFilterDate) : null,
      this.primengTableHelper.getSorting(this.dataTable),
      1,
      1
    ).subscribe(result => {
      this._fileDownloadService.downloadTempFile(result);
    });
  }
  getModels(event?: LazyLoadEvent) {
    if (this.primengTableHelper.shouldResetPaging(event)) {
      this.paginator.changePage(0);
      return;
    }

    this.primengTableHelper.showLoadingIndicator();

    this._alterServiceProxy
      .getAlterationInvoiceInfos(
        this.filterText,
        this.startFilterDate ? moment(this.startFilterDate) : null,
        this.endFilterDate ? moment(this.endFilterDate) : null,
        this.primengTableHelper.getSorting(this.dataTable),
        this.primengTableHelper.getMaxResultCount(this.paginator, event),
        this.primengTableHelper.getSkipCount(this.paginator, event)
      )
      .subscribe(result => {
        this.primengTableHelper.totalRecordsCount = result.totalCount;
        this.primengTableHelper.records = result.items;
        this.primengTableHelper.hideLoadingIndicator();
      });
  }

  deleteModel(id: string) {
    this.message.confirm('确认删除该文件吗？', isConfirmed => {
      if (isConfirmed) {
        this._alterServiceProxy.deleteAlteration(id).subscribe(() => {
          this.reloadPage();
          this.notify.success(this.l('SuccessfullyDeleted'));
        });
      }
    });
  }

  reloadPage(): void {
    this.paginator.changePage(this.paginator.getPage());
  }

  // onUpload(event) {
  //   let list = event as Array<any>;

  //   let productImportIntoList = list.map(el => {
  //     return {
  //       productName: el.产品名称,
  //       oldProductName: el.旧产品名称,
  //       productModel:  el.产品型号,
  //       productAM:  Number(el.手动自动=="AT"?1:el.手动自动=="MT"?2:0),
  //       carName:  el.汽车,
  //       produtTrait:  el.产品特征,
  //       mineFactory:  el.主要厂家
  //     };
  //   }) as Array<ProductImportIntoListDto>;
  //   this.message.confirm(
  //     '确认导入这' + list.length + '条数据吗？',
  //     this.l('AreYouSure'),
  //     isConfirmed => {
  //       if (isConfirmed) {
  //         this.primengTableHelper.showLoadingIndicator();
  //         this._productServiceProxy
  //           .importIntoProduct(productImportIntoList)
  //           .subscribe(result => {
  //             this.getModels();
  //             this.notify.success('导入数据成功');
  //           });
  //       }
  //     }
  //   );
  // }
}
